# GET	/goods	获取所有商品
# POST	/goods	增加商品
# GET	/goods/1	获取编号为1的商品
# PUT	/goods/1	修改编号为1的商品
# DELETE	/goods/1	删除编号为1的商品
from django.views.generic import View
from django.http.response import HttpResponse,JsonResponse

from booktest.models import BookInfo,HeroInfo
import json
from rest_framework.viewsets import ModelViewSet
from booktest.serializers import BookInfoSerializer,HeroInfoSerializer
# from .serializers import BookInforModelSerializer
#
#
# class BookAPIViewSet(ModelViewSet):
#
#     queryset = BookInfo.objects.all()
#
#     serializer_class = BookInforModelSerializer


# book = BookInfo.objects.get(id = 1)
#
# serializer = BookInfoSerializer(instance = book)
#
# serializer.data

# books = BookInfo.objects.all()
# serializer = BookInfoSerializer(instance = books,many=True)
# serializer.data



# hero = BookInfo.objects.get(id = 1)
# hero_serializer = HeroInfoSerializer(hero)
# hero_serializer.data




data = {'btitle':'小三国','bpub_data':'1999-11-10','bread':20,'comment':25}
serializer = BookInfoSerializer(data = data)
serializer.is_valid(raise_exception=True)

# serializer.is_valid()
# serializer.errors
# serializer.validated_data

serializer.save()



# class BooksAPIView(View):
#
#     def get(self,request):
#         books = BookInfo.objects.all()
#         book_list = []
#         for book in books:
#             book_dict = {
#                 'id':book.id,
#                 'btitle':book.btitle,
#                 'bpub_date':book.bpub_date,
#                 'bread':book.bread,
#                 'bcomment':book.bcomment,
#             }
#             book_list.append(book_dict)
#
#         return JsonResponse(book_list,safe = False)
#
#
#     def post(self,request):
#         json_str_bites =  request.body
#         json_str = json_str_bites.decode()
#         json_dict = json.loads(json_str)
#
#         book = BookInfo.objects.create(
#             btitle = json_dict['btitle'],
#             bpub_date = json_dict['bpub_data'],
#         )
#         book_dict = {
#             'id':book.id,
#             'btitle':book.btitle,
#             'bpub_date':book.bpub_date,
#             'bread': book.bread,
#             'bcomment':book.bcomment,
#         }
#
#         return JsonResponse(book_dict,status=201)
#
# class BookAPIView(View):
#
#     def get(self,request,pk):
#         try:
#             book = BookInfo.objects.get(id = pk)
#         except BookInfo.DoesNotExist:
#             return HttpResponse({'message':'pk不存在'},status=400)
#
#         book_dict = {
#             'id':book.id,
#             'btitle':book.btitle,
#             'bpub_date':book.bpub_date,
#             'bread':book.read,
#             'bcomment':book.bcomment,
#         }
#         return JsonResponse(book_dict)
#
#     def put(self,request,pk):
#         json_str_btyes = request.body
#         json_str = json_str_btyes.decode()
#         json_dict = json.loads(json_str)
#
#         try:
#             book = BookInfo.objects.get(id = pk)
#         except BookInfo.DoesNotExist:
#             return HttpResponse({'message':'pk不存在'},status = 400)
#         book.btitle = json_dict['btitle']
#         book.bpub_date = json_dict['bpub_data']
#         book.save()
#
#         book_dict = {
#             'id':book.id,
#             'btitle':book.btitle,
#             'bpub_date':book.bpub_date,
#             'bread':book.bread,
#             'bcomment':book.bcomment,
#         }
#         return JsonResponse(book_dict)
#
#
#     def delete(self,request,pk):
#         try:
#             book = BookInfo.objects.get(id = pk)
#         except BookInfo.DoesNotExist:
#             return HttpResponse({'message':'pkbucunzai'},status=400)
#         book.delete()
#         return HttpResponse(status=204)





























